﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class change_Detail_emp : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                if (day2.Length == 1)
                {
                    day2 = "0" + day2;
                }
                if (mount2.Length == 1)
                {
                    mount2 = "0" + mount2;
                }
                return (years2 + "-" + mount2 + "-" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            if (day3.Length == 1)
            {
                day3 = "0" + day3;
            }
            if (mount3.Length == 1)
            {
                mount3 = "0" + mount3;
            }
            return (years3 + "-" + mount3 + "-" + day3);
        }

        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years > 2300)
                {
                    years = years - 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }

        int id_emp, emp_id_add;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 3)
                    {
                        id_emp = Int32.Parse(Session["id_emp"].ToString());
                        try
                        {
                            string around = @"select *
                                from Table_Personnel
                                inner join Table_positions
                                    on Table_Personnel.Positions_id = Table_positions.Positions_id
                                inner join District
                                    on Table_Personnel.Personnel_district = District.DistrictId
                                inner join Amphur
                                    on Table_Personnel.Personnel_sub_district = Amphur.AmphurId
                                inner join Province
                                    on Table_Personnel.Personnel_province = Province.ProvinceId
                                where Table_Personnel.Personnel_id = " + id_emp + "";
                            SqlCommand sqlcomm = new SqlCommand();
                            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                            SqlConnection sqlconn = new SqlConnection(Connection);
                            SqlDataAdapter da = new SqlDataAdapter(around, sqlconn);
                            DataTable dt = new DataTable();
                            da.Fill(dt);
                            TextBox18.Text = dt.Rows[0]["Personnel_id"].ToString();
                            Session["emp_id_add"] = Int32.Parse(dt.Rows[0]["Personnel_id"].ToString());
                            DropDownList1.SelectedValue = dt.Rows[0]["Personnel_prefix"].ToString();
                            TextBox1.Text = dt.Rows[0]["Personnel_name"].ToString();
                            TextBox2.Text = dt.Rows[0]["Personnel_surname"].ToString();
                            DropDownList6.SelectedValue = dt.Rows[0]["Personnel_sex"].ToString();
                            TextBox4.Text = ConvestToDate_check(dt.Rows[0]["Personnel_birthday"].ToString());
                            TextBox5.Text = dt.Rows[0]["Personnel_home_no"].ToString();
                            TextBox6.Text = dt.Rows[0]["Personnel_village_no"].ToString();
                            TextBox7.Text = dt.Rows[0]["Personnel_lane"].ToString();
                            TextBox12.Text = dt.Rows[0]["Personnel_road"].ToString();
                            DropDownList2.SelectedValue = dt.Rows[0]["Personnel_province"].ToString().Trim();
                            DropDownList3.SelectedValue = dt.Rows[0]["Personnel_sub_district"].ToString().Trim();
                            DropDownList4.SelectedValue = dt.Rows[0]["Personnel_district"].ToString().Trim();
                            TextBox8.Text = dt.Rows[0]["Personnel_post"].ToString();
                            TextBox9.Text = dt.Rows[0]["Personnel_mail"].ToString();
                            TextBox11.Text = dt.Rows[0]["Personnel_tel"].ToString();
                            TextBox3.Text = dt.Rows[0]["Personnel_identification"].ToString();
                            DropDownList5.SelectedValue = dt.Rows[0]["Positions_id"].ToString();
                        }
                        catch (Exception)
                        {

                        }
                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            emp_id_add = Int32.Parse(Session["emp_id_add"].ToString());
            try
            {                
                var q = from p in db.Table_Personnels
                        where p.Personnel_id == emp_id_add
                        select p;
                q.ToList().ForEach(c => c.Personnel_prefix = DropDownList1.SelectedItem.ToString());
                q.ToList().ForEach(c => c.Personnel_name = TextBox1.Text);
                q.ToList().ForEach(c => c.Personnel_surname = TextBox2.Text);
                q.ToList().ForEach(c => c.Personnel_identification = TextBox3.Text);
                q.ToList().ForEach(c => c.Personnel_sex = DropDownList6.SelectedItem.ToString());
                q.ToList().ForEach(c => c.Personnel_birthday = ConvestToDate(TextBox4.Text));
                q.ToList().ForEach(c => c.Personnel_home_no = TextBox5.Text);
                q.ToList().ForEach(c => c.Personnel_village_no =TextBox6.Text);
                q.ToList().ForEach(c => c.Personnel_lane = TextBox12.Text);
                q.ToList().ForEach(c => c.Personnel_road = TextBox7.Text);
                q.ToList().ForEach(c => c.Personnel_province = Int32.Parse(DropDownList2.SelectedValue.ToString()));
                q.ToList().ForEach(c => c.Personnel_sub_district = Int32.Parse(DropDownList3.SelectedValue.ToString())); 
                q.ToList().ForEach(c => c.Personnel_district = Int32.Parse(DropDownList4.SelectedValue.ToString()));
                q.ToList().ForEach(c => c.Personnel_post = TextBox8.Text); 
                q.ToList().ForEach(c => c.Personnel_mail = TextBox9.Text); 
                q.ToList().ForEach(c => c.Personnel_tel = TextBox11.Text); 
                q.ToList().ForEach(c => c.Positions_id = Int32.Parse(DropDownList5.SelectedValue.ToString()));                
                db.SubmitChanges();
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('แก้ไขข้อมูลพนักงานแล้ว');", true);
                //Response.Write(@"<script language='javascript'>alert('แก้ไขข้อมูลพนักงานแล้ว');</script>");
                Session["emp_id"] = emp_id_add;
                Response.Redirect("Detail_emp_admin.aspx");
            }
            catch (Exception)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถแก้ไขข้อมูลพนักงานได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถแก้ไขข้อมูลพนักงานได้');</script>");
                throw;
            }           
        
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Search_emp.aspx");
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList3.Items.Clear();
            DropDownList4.Items.Clear();
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList4.Items.Clear();
        }  
       
    }
}